// pages/addrList/index.js
var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var app = getApp();

Page(common.extendPage(PageBase, {
  data: {
    addrList: [],
    loading: true
  },
  onLoad(options) {
    this._options = options;

  },
  onShow() {
    this.getAddr();
  },
  _del(evt) {
    const {
      currentBar,
      currentId
    } = evt.currentTarget.dataset;
    console.log(currentBar);
    // const addrList = this.data.addrList;

    // addrList.splice(currentBar, 1);
    // this.setData({
    //   addrList
    // })
    // console.log(this.data.addrList);

    var paramsUrl = '/public/api/user/deladdres';
    var paramsData = {
      uid: wx.getStorageSync('uid'),
      id: currentId
    }
    ajax.P({
      url: paramsUrl,
      data: paramsData,
      success: (res) => {
        util.tips('删除成功', () => {
          this.getAddr();
        })
      }
    })
  },
  _selectedAddr(evt) {
    var {
      currentBar
    } = evt.currentTarget.dataset;
    var addrList = this.data.addrList;
    if (this._options.page == 'confirm') {
      wx.setStorageSync('currentAddr', addrList[currentBar]);
      wx.navigateBack();
    }
    return false;
  },
  /**
   * 获取地址列表
   */
  getAddr() {
    var paramsUrl = '/public/api/user/addresslist';
    var paramsData = {
      uid: wx.getStorageSync('uid')
    }
    ajax.P({
      url: paramsUrl,
      data: paramsData,
      success: (res, isLoadcb) => {
        res.data.forEach((item, index) => {
          item.addrText = item.area.join('') + item.address;
        })
        this.setData({
          addrList: res.data,
          loading: false
        })
        isLoadcb && isLoadcb();
      },
      fail: (res) => {

      }
    })
  }
}));